package user

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"go-interview-forum/resp"
	"go-interview-forum/service/user"
	"strconv"
)

type ReqUserInfo struct {
	Nickname string `json:"nickname"`
	Avatar   string `json:"avatar"`
	Mobile   string `json:"mobile"`
}

type From struct {
	From string `json:"from"`
}

func GetUserInfo(c *gin.Context) {
	var from From
	err := c.ShouldBind(&from)
	if err != nil {
		resp.ServerErrorResp(c, resp.SERVER_ERR, "系统错误")
		return
	}
	userId, exists := c.Get("userId")
	if !exists {
		resp.ServerErrorResp(c, resp.SERVER_ERR, "系统错误")
		return
	}

	id, err := strconv.Atoi(userId.(string))
	if err != nil {
		resp.ServerErrorResp(c, resp.SERVER_ERR, "系统错误")
		return
	}

	info, err := user.GitUserInfo(id)
	if err != nil {
		resp.ServerErrorResp(c, resp.SERVER_ERR, err.Error())
		return
	}

	res := ReqUserInfo{
		Nickname: info.Nickname,
		Avatar:   info.Avatar,
	}

	if from.From != "" {
		res.Mobile = fmt.Sprintf("%s****%s", info.Mobile[:3], info.Mobile[7:])
	}

	resp.SuccessResp(c, res)

}
